// /pages/product_detail/product_detail.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;

Page({
  data: {
    spell_info:null,
    spell_goods_list:[],

    psons:0,
    times:'',
    timer:null,
    
    psn:''
  },
  //拼团商品团详情
  //index.php?act=other&op=spell_info
  spell_info(goods_id){
    let that = this;
    // let k = wx.getStorageSync('key');
    // let uid = wx.getStorageSync('userId');
    // console.log(k, uid, 909090);
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=spell_info',
      data: {
        act: 'other',
        op: 'spell_info',
        goods_id: goods_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        console.log('拼团商品团详情:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.info;
            console.log('拼团商品团详情:==>', res);
            let psons = parseInt(res.virtual_quantity) + parseInt(res.buy_quantity);

            let times = res.count_down;
            if (times > 0) {
              that.getDuration(times);
              that.data.timer = setInterval(function () {

                if (that.data.time == 1) {
                  clearInterval(that.data.timer);
                }
                times--;
                that.getDuration(times);
                console.log(111);
              }, 1000)
            }
            that.setData({
              spell_list: res,
              psons: psons
            })
          }
        }
      }
    });
  },
  getDuration(second) {
    var days = Math.floor(second / 86400);
    var hours = Math.floor((second % 86400) / 3600);
    var minutes = Math.floor(((second % 86400) % 3600) / 60);
    var seconds = Math.floor(((second % 86400) % 3600) % 60);
    var duration = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
    // if (days>0){
    //   var duration = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
    // } else if (hours > 0) {
    //   var duration = hours + "小时" + minutes + "分" + seconds + "秒";
    // } else if (minutes>0){

    // }
    this.setData({
      times: duration
    })
  },
  //index.php?act=other&op=spell_goods_list
  //拼团-某一个商品正在进行的团（大家都在拼团）
  spell_goods_list(goods_id){
    let that = this;
    // let k = wx.getStorageSync('key');
    // let uid = wx.getStorageSync('userId');
    console.log(goods_id);
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=spell_goods_list',
      data: {
        act: 'other',
        op: 'spell_goods_list',
        goods_id: goods_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method:"POST",
      success: function (result) {
        console.log('大家都在拼团:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.list;
            console.log('大家都在拼团:==>', res);
            that.setData({
              spell_goods_list: res
            })
          }
        }
      }
    });
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      psn:options.psn
    });
    if (options.goods_id){
      this.spell_info(options.goods_id);
      this.spell_goods_list(options.goods_id)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
  //   if (ops.from === 'button') {
  //     // 来自页面内转发按钮
  //     console.log(ops.target)
  //   }
  //   return {
  //     title: '邀请好友签到，赚积分',
  //     imageUrl: 'http://xxxx',//图片地址
  //     path: '/pages/make_money/make_money?uid=123&&type=xxx',// 用户点击首先进入的当前页面
  //     success: function (res) {
  //       // 转发成功
  //       console.log("转发成功:");
  //     },
  //     fail: function (res) {
  //       // 转发失败
  //       console.log("转发失败:");
  //     }
  //   }

  }
})